<?php

namespace App\Repositories\Criteria\Common;

use App\Repositories\Contracts\CriteriaInterface;
use App\Repositories\Contracts\RepositoryInterface as Repository;
use App\Repositories\Contracts\RepositoryInterface;
use App\Repositories\Criteria\Criteria;

class TimeBetween extends Criteria
{
    /**
     * 数据库字段名
     *
     * @var
     */
    protected $key;

    /**
     * 开始时间
     *
     * @var
     */
    protected $start;

    /**
     * 结束时间
     *
     * @var
     */
    protected $end;

    /**
     * TimeBetween constructor.
     * @param $key
     * @param $start
     * @param $end
     */
    public function __construct($key, $start, $end)
    {
        $this->key = $key;
        $this->start = $start;
        $this->end = $end;
    }

    /**
     * @param $model
     * @param RepositoryInterface $repository
     * @return mixed
     */
    public function apply($model, Repository $repository)
    {
        $query = $model->where($this->key, '>=', $this->start)
            ->where('pub_time', '<=', $this->end);
        return $query;
    }
}